/*   The MIT License
 *   
 *   Carina Engine
 *   Copyright (c) 2009 2010 2011 2012 Zdravko Velinov
 *   
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */

#ifndef DX_LIBRARY_HH
#define DX_LIBRARY_HH

#include "carina/common/global.hh"
#include "carina/renderer-base.hh"
#include "carina/library.hh"
#include "carina/common/exception.hh"

#include <iomanip>
#include <sstream>

#ifdef __MINGW32__
#include <specstrings.h>

#ifndef WINAPI_INLINE
#   define WINAPI_INLINE inline
#endif

typedef unsigned char UINT8;
#endif

#ifdef __MINGW32__
#   pragma GCC diagnostic push
#   pragma GCC diagnostic ignored "-pedantic"
#   pragma GCC diagnostic ignored "-Wsign-compare"
#   define __in
#   define __in_opt
// We are expecting that when we have this thing defined the rest of the stuff
// would be also defined. That's some hacking to remove some symbols that are
// not used by GCC.
#   ifndef __in_ecount
#       define __in_bcount(x)
#       define __in_bcount_opt(x)
#       define __in_ecount(x)
#       define __in_ecount_opt(x)
#       define __out_bcount(x)
#       define __out_ecount(x)
#       define __out_ecount_opt(x)
#       define __out_ecount_part(x)
#       define __out_ecount_part_opt(x, y)
#       define __out_bcount_opt(x)
#   endif
#   define __out
#   define __out_opt
#   define __inout
#   define __inout_opt
#   define __struct_bcount(x)
#   define __field_ecount_opt(x)
#endif

// Horrendous headers ahead
#pragma warning(push)
#	pragma warning(disable : 4005)
#	include <D3D11.h>
#	include <D3Dcompiler.h>
#pragma warning(pop)

#ifdef __MINGW32__
#   pragma GCC diagnostic pop
#endif

#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>


namespace Carina
{
#ifndef _MSC_VER
#   define UUIDOF(type) (REFIID)IID_##type##MS
#else
#   define UUIDOF(type) __uuidof(type)
#endif

void CheckHResult(HRESULT hr);

#ifndef NDEBUG
#   define HR_ASSERT(expr) CheckHResult(expr)
#else
#   define HR_ASSERT(expr) (expr)
#endif

template<class T>
class ComRAII
{
    T* m_Ptr;
public:
    ComRAII() : m_Ptr(nullptr) {}
//  ComRAII(T* p) : m_Ptr(p) {}
    ComRAII(const ComRAII& raii)
        :   m_Ptr(raii.m_Ptr) { if(m_Ptr) m_Ptr->AddRef(); }
     ~ComRAII()
    {
         release();
    }

    ComRAII& operator=(const ComRAII& raii)
    {
        release();
        m_Ptr = raii.m_Ptr;
        if(m_Ptr)
            m_Ptr->AddRef();
        return *this;
    }

    ComRAII& operator=(T* const ptr)
    {
        release();
        m_Ptr = ptr;
        return *this;
    }

    void release()
    {
        if(m_Ptr)
        {
            m_Ptr->Release();
            m_Ptr = nullptr;
        }
    }

    T* get() { return m_Ptr; }

    T** operator&(){ return &m_Ptr; }
    operator T*() const { return m_Ptr; }
    T& operator*() { return *m_Ptr; }
    T* operator->() { return m_Ptr; }

    T* const * operator&() const { return &m_Ptr; }
    const T& operator*() const { return *m_Ptr; }
    const T* operator->() const { return m_Ptr; }
};

#define D3D_LOAD_FUNCTION(lib, func, name) \
    name##_MS = reinterpret_cast<func>(lib.getProcAddress(TO_STRING(name))); \
    if(!name##_MS) \
        return false;

class DXLibrary: public RenderingLibrary
{
    Library m_D3D11Lib;
    Library m_D3DCompilerLib;
public:
    DXLibrary();

    static bool check();

    bool init();

    virtual WindowImplPtr createWindowImpl();
};

/////////////////////
// D3D11 functions //
/////////////////////
extern PFN_D3D11_CREATE_DEVICE_AND_SWAP_CHAIN D3D11CreateDeviceAndSwapChain_MS;
//extern pD3DCompile D3DCompile_MS;

/////////
// IID //
/////////
#ifndef _MSC_VER
extern const GUID IID_ID3D11Texture2DMS;
#endif
}

#endif /* DX_LIBRARY_HH */
